<template>
  <div v-if="detail">
    <el-dialog title="退款" :visible.sync="dialogVisible" width="600px" @close="close">
      <el-form
        ref="form"
        label-suffix="："
        :model="form"
        label-width="100px"
        :rules="rules"
        :close-on-click-modal="false"
        size="small"
      >
        <el-form-item label="支付金额">￥{{ detail.order.payFee }}</el-form-item>
        <el-form-item label="退款金额">￥{{ detail.order.payFee }}</el-form-item>
        <el-form-item label="退款备注" prop="remark">
          <el-input v-model.trim="form.remark" maxlength="50" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" round @click="hide">取 消</el-button>
        <el-button size="small" round type="primary" @click="openInnerdialog">退款</el-button>
      </div>

      <!--支付密码弹框-->
      <el-dialog
        title="请输入您的退款密码"
        :visible.sync="dialogVisibleInset"
        width="500px"
        append-to-body
        :close-on-click-modal="false"
      >
        <!--密码输入框-->
        <div
          v-if="dialogVisibleInset"
          v-loading="loading"
          element-loading-text="退款中"
          class="dialog-inset"
        >
          <div class="refund-num">
            <div>退款金额</div>
            <div>￥{{ detail.order.payFee }}</div>
          </div>
          <div class="refund-psw">
            <PassWordBox ref="passWordBox" @sendPsw="sendPsw" />
          </div>
        </div>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import { refundOrder } from '../../api/record'
export default {
  data() {
    return {
      /**
         * 校验字段相关
         */
      rules: {
        remark: [
          {
            required: true,
            message: '请输入退款备注'
          }
        ]
      },
      // 数据
      dialogVisibleInset: null,
      // 配置
      resolve: null,
      reject: null,
      dialogVisible: null,
      loading: false,
      // 数据
      detail: '',
      form: {
        deviceId: '',
        operatorId: '',
        orderNo: '',
        refundPwd: '',
        remark: ''
      }
    }
  },
  methods: {
    open(detail) {
      this.detail = detail
      this.form.orderNo = detail.order.orderNo
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.form = {
        deviceId: '',
        operatorId: '',
        orderNo: '',
        refundPwd: '',
        remark: ''
      }
      this.loading = false
      this.$refs.form.resetFields()
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    // 打开密码弹框
    openInnerdialog(formName) {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = false
          this.dialogVisibleInset = true
        }
      })
    },

    // 提交退款
    sendPsw(psw) {
      const params = Object.assign({}, this.form)
      // let md5Password = this.$md5(String(psw));
      params.refundPwd = psw
      this.loading = true
      refundOrder(params)
        .then(res => {
          this.$notify({
            title: '成功',
            message: '退款成功',
            type: 'success',
            duration: 2000,
            offset: 40
          })
          this.$refs.passWordBox.defaultPsw()
          this.dialogVisibleInset = false
          this.resolve && this.resolve()
          this.hide()
        })
        .catch(err => {
          this.$refs.passWordBox.defaultPsw()
        })
        .finally(() => {
          this.loading = false
        })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  /* 内层弹框 */
  .dialog-inset {
    /* 退款金额*/
    .refund-num {
      text-align: center;
      div:first-child {
        padding: 20px 0;
        font-size: 18px;
      }
      div:last-child {
        font-size: 30px;
      }
    }
    /* 密码输入框 */
    .refund-psw {
      padding: 20px 0;
    }
  }
</style>
